package com.whq.controller;

import com.whq.entity.Member;
import com.whq.service.CreditInfoService;
import com.whq.service.MemberService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.beans.IntrospectionException;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;



/**
 * Created by qpx on 2018/4/19.
 */
@Controller
@RequestMapping("/creditInfo")
public class PoiExcelController {


    @Autowired
    MemberService memberService;

    @Autowired
    CreditInfoService creditInfoService;

    @RequestMapping("/poiPage")
    public String poiPage(){
        return "poiPage";
    }


    @ResponseBody
    @RequestMapping(value="/uploadPayerCreditInfoExcel",method={RequestMethod.GET,RequestMethod.POST})
    public  void uploadPayerCreditInfoExcel(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("upfile");
        if(file.isEmpty()){
            throw new Exception("文件不存在！");
        }
        InputStream in = file.getInputStream();
        creditInfoService.uploadPayerCreditInfoExcel(in,file);
        in.close();
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文信息乱码
        out = response.getWriter();
        out.print("文件导入成功！");
        out.flush();
        out.close();
    }

    /**
     * 导出excel
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping(value = "/downLoadPayerCreditInfoExcel", method = RequestMethod.GET)
    @ResponseBody
    public void downLoadPayerCreditInfoExcel(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        response.reset();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");
        String dateStr = sdf.format(new Date());

        Map<String,Object> map=new HashMap<String,Object>();
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" +dateStr+".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        XSSFWorkbook workbook=null;
        try {
            //导出Excel对象
            workbook = creditInfoService.exportPayerCreditInfoExcel();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        OutputStream output;
        try {
            output = response.getOutputStream();

            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            bufferedOutPut.flush();
            workbook.write(bufferedOutPut);
            bufferedOutPut.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
